<?php
declare(strict_types=1);

namespace App\Model;

use App\Helper\ModuleHelper;

/**
 * @property int $id
 * @property string $name
 * @property string $module
 * @property array $module_available
 */
class AdminRole extends MyBaseModel
{
    public $timestamps = false;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'admin_role';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'module'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'name' => 'string'];

    protected $appends = [
        'module_available',
    ];

    public function getModuleAvailableAttribute(): array
    {
        if (!$this->module) {
            return [];
        }
        $array = json_decode($this->module, true);
        if (!$array) {
            return [];
        }
        return ModuleHelper::checkAvailable($array);
    }
}
